package com.hb.trade.server.center.au.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hb.core.pojo.center.au.entity.ApiInfo;
import com.hb.core.pojo.parent.MPBaseController;
import com.hb.core.pojo.parent.PageResult;
import com.hb.trade.server.center.au.service.ApiInfoService;
import org.fzzn.core.model.enums.MsgEnum;
import org.fzzn.core.model.msg.Message;
import org.fzzn.core.util.MsgUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 项目中的所有接口信息 前端控制器
 * </p>
 *
 * @author zhaojk
 * @since 2022-03-22
 */
@RestController
public class InfoApiController extends MPBaseController<ApiInfo, ApiInfoService> {


    public InfoApiController(ApiInfoService service) {
        super(service);
    }

    @PostMapping("/apiInfo/updateApis")
    public Message<Boolean> updateApis(@RequestBody List<ApiInfo> apis) {
        if (CollUtil.isEmpty(apis)) {
            return MsgUtil.msg(MsgEnum.INCORRECT_PARAM);
        }
        for (ApiInfo api : apis) {
            String apiName = api.getApiName();
            if (StrUtil.isEmpty(apiName)) {
                continue;
            }
            boolean exists = service.exists(apiName);
            if (!exists) {
                api.buildBaseData(1);
                service.save(api);
            }
        }
        return MsgUtil.success(true);
    }

    @PostMapping("/apiInfo/pageApiInfos")
    public Message<PageResult<ApiInfo>> pageApiInfos(@RequestBody ApiInfo apiInfo) {
        Page<ApiInfo> pages = apiInfo.pages();
        service.page(pages);
        return MsgUtil.success(PageResult.get(pages));
    }
}
